/**
 * 加载状态
 * @param delay 加载的最小时长
 * @returns
 */
export const useLoading = (
  delay = 400
): {
  status: boolean
  startLoading: () => void
  closeLoading: () => void
} => {
  let loadingStartTime = 0

  const loading = reactive({
    status: false,
    startLoading: () => {
      loadingStartTime = Date.now()
      loading.status = true
    },
    closeLoading: () => {
      const loadingTime = Date.now() - loadingStartTime
      if (loadingTime > delay) {
        loading.status = false
      } else {
        const timer = setTimeout(() => {
          loading.status = false
          clearTimeout(timer)
        }, delay - loadingTime)
      }
    }
  })

  return loading
}
